﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VX.DBTransfer.Model.Entity
{
    public class TableEntity
    {
        public string TableName { get; set; }

        public List<ColumnEntity> Columns { get; set; }

        public int ColumnCount { get { return Columns.Count; } }

        public bool IsPKIdentity
        {
            get
            {
                return Columns.Exists(n => n.IsIdentity);
            }
        }
        public TableEntity()
        {
            Columns = new List<ColumnEntity>();
        }

        public static bool IsTablesSame(List<TableEntity> sourceEntity, List<TableEntity> targetEntity)
        {
            sourceEntity.ForEach(n => n.Columns = n.Columns.OrderBy(c => c.ColumnName).ToList());
            targetEntity.ForEach(n => n.Columns = n.Columns.OrderBy(c => c.ColumnName).ToList());
            sourceEntity = sourceEntity.OrderBy(n => n.TableName).ToList();
            targetEntity = targetEntity.OrderBy(n => n.TableName).ToList();
            bool result = true;
            if (sourceEntity.Count != targetEntity.Count)
                result = false;
            if (result)
            {
                int listCount = sourceEntity.Count;
                for (int i = 0; i < listCount; i++)
                {
                    if (sourceEntity[i].TableName != targetEntity[i].TableName)
                    {
                        result = false;
                        break;
                    }
                    if (sourceEntity[i].ColumnCount != targetEntity[i].ColumnCount)
                    {
                        result = false;
                        break;
                    }
                    if (string.Join("", sourceEntity[i].Columns.Select(n => n.ColumnName)) != string.Join("", targetEntity[i].Columns.Select(n => n.ColumnName)))
                    {
                        result = false;
                        break;
                    }
                }
            }
            return result;
        }

        public static List<TableEntity> SelectDifferentTables(List<TableEntity> sourceEntity, List<TableEntity> targetEntity)
        {
            sourceEntity.ForEach(n => n.Columns = n.Columns.OrderBy(c => c.ColumnName).ToList());
            targetEntity.ForEach(n => n.Columns = n.Columns.OrderBy(c => c.ColumnName).ToList());
            sourceEntity = sourceEntity.OrderBy(n => n.TableName).ToList();
            targetEntity = targetEntity.OrderBy(n => n.TableName).ToList();
            List<TableEntity> result = new List<TableEntity>();
            for (int i = 0; i < sourceEntity.Count; i++)
            {
                if (targetEntity.Count(n => n.TableName == sourceEntity[i].TableName) == 0)
                {
                    result.Add(sourceEntity[i]);
                }
                else
                {
                    TableEntity tempEntity = targetEntity.First(n => n.TableName == sourceEntity[i].TableName);
                    if (string.Join("", tempEntity.Columns.Select(n => n.ColumnName)) != string.Join("", sourceEntity[i].Columns.Select(n => n.ColumnName)))
                    {
                        result.Add(sourceEntity[i]);
                    }
                }
            }
            return result;
        }
    }
}
